
// map.addControl(new MyCustomControl());

var QMapCM = QOverlayControl.extend('QMapCM', {

	map : null,
	lastMapMove : 0,
	lastMapMoveTriggered : 0,
	showLargeMapControl : false,
	showScaleControl : false,
	showOverviewMapControl : false,
	_tmp_timeout : null,
	_map_geocoder : null,
	_api_key : "fffd112c055c4495900e61855991f251",
	_style_id : 997,
	_search_overlay : null,
	_saved_bounds_user : null,
	_markers : [],
	_markersHidden : false,
	imageMode : false,

	_default_bounds : null,

	_mapControls : [],
	_mapControlsVisible : false,
	
	_enalarge_control : null,
	_enalarge_control_visible : false,
	_is_enlarged : false,
	_enlarge_saved_pos : {},
	_map_actions_enabled : true,
	_enlargeOnMarker : null,
	_texts : {expand : "Expand", close : "Close"},
	
	_code_event : false,

	load : function()
	{
		// jQuery("div", this.domElement).css("width", "100%").css("height", "100%");

		if (this._style_id)
			var cloudmade = new CM.Tiles.CloudMade.Web({key: this._api_key, styleId: this._style_id});
		else
			var cloudmade = new CM.Tiles.CloudMade.Web({key: this._api_key});

		var inner_full_id = this.fullId + "__CMap";
		
		// alert(inner_full_id);

		jQuery(this.domElement).find("div").attr("id", inner_full_id);
		jQuery("#" + inner_full_id, this.domElement).css("width", "100%").css("height", "100%");

		// CM.Tiles.OpenStreetMap.Cycle()

		// this.map = new CM.Map(inner_full_id, cloudmade);
		this.map = new CM.Map(inner_full_id, cloudmade);
		this.map._jsCtrl = this;

		this.mapSetCenter(new CM.LatLng(45.943165, 24.96676), 6);
		// 43.62244x20.26102 48.26389x29.6725
		
		this.map.checkResize = function () {};

		jQuery(this.domElement).bind( "ev_sizeChanged", this, function (e) {e.data.updateBoundsChanged();} );

		var ev_callback = function (latlng)
			{
				/*if (this._code_event)
					return;*/
				
				if (this._tmp_timeout)
					clearTimeout(this._tmp_timeout);

				var ref_this = this;
				this.lastMapMove = (new Date()).getTime();
				this._tmp_timeout = setTimeout(function () {ref_this.checkMoveEndedEvent();}, 1000);
			};
		// moveend
		// CM.Event.addListener(this.map, 'moveend', ev_callback, this);
		CM.Event.addListener(this.map, 'zoomend', ev_callback, this);
		CM.Event.addListener(this.map, 'dragend', ev_callback, this);
		// CM.Event.addListener(this.map, 'dblclick', ev_callback, this);
		// CM.Event.addListener(this.map, 'dragend', ev_callback, this);
		// CM.DomEvent.addListener(this.domElement, 'mousewheel', ev_callback, this);
		// CM.Event.addListener(this.map, 'moveend', ev_callback, this);

		var ref_this = this;

		jQuery(this).bind("ev_ImageControlChanged", function (e) {ref_this.onUpdateDisplayImages(e);});
		jQuery(this).bind("ev_LayerControlChanged", function (e) {ref_this.onUpdateDisplayLayer(e);});

		jQuery(this).bind("ev_CenterControlChanged", function (e) {ref_this.zoomToDefaultBounds();});
		
		jQuery(this).bind("ev_EnlargeControlChanged", function (e) {ref_this.switchEnlargeState();});

		// this.map.addControl(new CM.LargeMapControl());

		this._mapControls[this._mapControls.length] = new CM.SmallMapControl();
		this._mapControls[this._mapControls.length] = new QMapCMLayerControl();
		this._mapControls[this._mapControls.length] = new QMapCMCenterControl();

		this.showMapControls();

		this.map.disableScrollWheelZoom();
		// CM.Map.disableScrollWheelZoom();
		/*
		this.map.addControl(new CM.SmallMapControl());
		// this.map.addControl(new QMapCMImageControl());
		this.map.addControl(new QMapCMLayerControl());
		this.map.addControl(new QMapCMCenterControl());
		*/

		// create info window our way

 		jQuery(this.domElement).append("<div id='" + this.getOverlayInfoWindowId() + "' class='" + this.getOverlayInfoWindowId() + "'><!-- <a href='#' " +
 			"class='" + this.getOverlayInfoWindowId() + "-close'></a>-->" +
 			"<img src='" + this.resPath + "info-tip-right.png' class='" + this.getOverlayInfoWindowId() + "-tip'>" +
 			"<div class='" + this.getOverlayInfoWindowId() + "-content'>&nbsp;</div>" +
 			"</div>");
 			
 		var max_z_index = 100000001;
 		if (window.GetQWebPage)
 			max_z_index = GetQWebPage().MaxZIndex;

 		this.getOverlayInfoWindow().css("position", "absolute").css("z-index", max_z_index).css("display", "none").css("width", "auto");
 		this.getOverlayInfoWindow().find("." + this.getOverlayInfoWindowId() + "-content").css("background", "none repeat scroll 0 0 white").css("border", "1px solid #666666"
 			).css("padding", "10px");
 		this.getOverlayInfoWindow().find("." + this.getOverlayInfoWindowId() + "-tip").css("display", "block").css(
 			"overflow", "hidden").css("position", "absolute").css("right", "-9px").css("top", "8px");
 		this.getOverlayInfoWindow().find("." + this.getOverlayInfoWindowId() + "-close").css("background", "url('" + this.resPath + "info-close.png') repeat scroll 0 0 transparent"
 			).css("display", "inline").css("float", "right").css("height", "15px").css("margin", "6px").css("width", "15px");

 		// CM.DomEvent.removeListener(this.map.getContainer(), 'mousewheel', ev_callback, this);
		// this.jQuery(".wml-map-layer").unbind("mousewheel");
		
		/*
		if (jQuery.browser.msie)
		{
			var browser_version = parseFloat(jQuery.browser.version);
			
			if (browser_version < 8.0)
			{
				// alert(browser_version);
				var z_index = 100000;
				jQuery("*", this.domElement).each(
					function ()
					{
						// alert("div: " + jQuery(this).attr("class") + " | z-index: " + jQuery(this).css("z-index"));
						jQuery(this).css("z-index", "" + z_index);
						z_index -= 10;
					});
				jQuery(this.domElement).each(
					function ()
					{
						// alert("div: " + jQuery(this).attr("class") + " | z-index: " + jQuery(this).css("z-index"));
						jQuery(this).css("z-index", "" + z_index);
						z_index -= 10;
					});
			}
		}
		*/
	},
	
	switchEnlargeState : function(set_marker, latitude, longitude, zoom, marker_title)
	{
		if (this._enlargeOnMarker)
		{
			try { this.map.removeOverlay(this._enlargeOnMarker); } catch (ex) {}
			this._enlargeOnMarker = null;
		}

		if (this._is_enlarged)
		{
			this._pause_overlay = false;
		
			jQuery(this.domElement).css({position: this._enlarge_saved_pos.position, 
				width: this._enlarge_saved_pos.width, 
				height: this._enlarge_saved_pos.height, 
				top: this._enlarge_saved_pos.top, 
				left: this._enlarge_saved_pos.left,
				border: this._enlarge_saved_pos.border});
			
			if (!this._enlarge_saved_pos._map_actions_enabled)
				this.disableMapActions();
				
			this.map.checkResize();
			this.mapSetCenter(this._enlarge_saved_pos.map_center, this._enlarge_saved_pos.map_zoom);
			
			this._enalarge_control.setCaption(this._texts["expand"]);
			this._is_enlarged = false;
		}
		else
		{
			// must avoid repositioning for a while !
			this.showEnalargeControl();
			
			this._pause_overlay = true;
			this._enlarge_saved_pos = {};

			this._enlarge_saved_pos.position = jQuery(this.domElement).css("position");
			this._enlarge_saved_pos.width = jQuery(this.domElement).css("width");
			this._enlarge_saved_pos.height = jQuery(this.domElement).css("height");
			this._enlarge_saved_pos.top = jQuery(this.domElement).css("top");
			this._enlarge_saved_pos.left = jQuery(this.domElement).css("left");
			this._enlarge_saved_pos.border = jQuery(this.domElement).css("border");
			this._enlarge_saved_pos.map_center = this.map.getCenter();
			this._enlarge_saved_pos.map_zoom = this.map.getZoom();
			this._enlarge_saved_pos._map_actions_enabled = this._map_actions_enabled;

			jQuery(this.domElement).css({position: "fixed", width: "90%", height: "90%", top: "5%", left: "5%",
				border: "5px solid #A0C6E2"});
			
			if (!this._map_actions_enabled)
				this.enableMapActions();
				
			this.map.checkResize();
			if (set_marker)
			{
				this.mapSetCenter(new CM.LatLng(latitude, longitude), zoom);

				// alert(zoom + "x" + latitude + "x" + longitude + "x" + marker_title);

				this._enlargeOnMarker = new CM.Marker(new CM.LatLng(latitude, longitude), { title: marker_title});
				this.map.addOverlay(this._enlargeOnMarker);
			}
			else
				this.mapSetCenter(this._enlarge_saved_pos.map_center, this._enlarge_saved_pos.map_zoom);
				
			this.map.checkResize();
				
			this._enalarge_control.setCaption(this._texts["close"]);
			this._is_enlarged = true;
		}
	},
	
	setEnlargeState : function (enlarge, set_marker, latitude, longitude, zoom, marker_title)
	{
		if (enlarge && (!this._is_enlarged))
			this.switchEnlargeState(set_marker, latitude, longitude, zoom, marker_title);
		else if ((!enlarge) && (this._is_enlarged))
			this.switchEnlargeState(set_marker, latitude, longitude, zoom, marker_title);
	},
	
	enlargeOnMarker : function (latitude, longitude, zoom, marker_title)
	{
		this.setEnlargeState(true, true, latitude, longitude, zoom, marker_title);
	},
	
	showEnalargeControl : function()
	{
		if (!this._enalarge_control)
			this._enalarge_control = new QMapCMEnlargeControl();
			
		if (!this._enalarge_control_visible)
		{
			this.map.addControl(this._enalarge_control);
			this._enalarge_control_visible = true;
		}
	},
	
	hideEnalargeControl : function()
	{
		if (!this._enalarge_control)
			return;
			
		if (this._enalarge_control_visible)
		{
			this.map.removeControl(this._enalarge_control);
			this._enalarge_control_visible = false;
		}
	},

	hideMapControls : function(keep_copyright)
	{
		if (this._mapControlsVisible)
		{
			if (!(keep_copyright && (keep_copyright == true)))
				jQuery(".wml-copyright", this.domElement).hide();
			for (var i = 0; i < this._mapControls.length; i++)
				this.map.removeControl(this._mapControls[i]);

			this._mapControlsVisible = false;
		}
		this.hideEnalargeControl();
	},

	showMapControls : function()
	{
		if (!this._mapControlsVisible)
		{
			jQuery(".wml-copyright", this.domElement).show();
			for (var i = 0; i < this._mapControls.length; i++)
				this.map.addControl(this._mapControls[i]);

			this._mapControlsVisible = true;
		}
		if (this._enalarge_control_visible)
			this.showEnalargeControl();
	},

	zoomToDefaultBounds : function()
	{
		this.mapZoomToBounds(this._default_bounds);
	},
	
	setDefaultBounds : function(bounds)
	{
		this._default_bounds = bounds;
	},
	
	getDefaultBounds : function(bounds)
	{
		return this._default_bounds;
	},

	checkStrictBounds : function()
	{
		// alert("_strict_bounds");
		if (this._strict_bounds)
		{
			var nb = this.map.getBounds();
			var ob = this._strict_bounds;
			// contains(<CM.LatLngBounds> bounds)
			// getSouthWest() getNorthEast()
			var reposition = false;

			if (nb.getSouthWest().lat() < ob.getSouthWest().lat()) reposition = true;
			if (nb.getSouthWest().lng() < ob.getSouthWest().lng()) reposition = true;

			if (nb.getNorthEast().lat() > ob.getNorthEast().lat()) reposition = true;
			if (nb.getNorthEast().lng() > ob.getNorthEast().lng()) reposition = true;

			/*alert(nb.getSouthWest().lat() + " x " + nb.getSouthWest().lng() + " x " +
				nb.getNorthEast().lat() + " x " + nb.getNorthEast().lng() + " || " +
				ob.getSouthWest().lat() + " x " + ob.getSouthWest().lng() + " x " +
				ob.getNorthEast().lat() + " x " + ob.getNorthEast().lng());*/

			if (reposition)
			{
				this.mapZoomToBounds(this._strict_bounds);
				// alert("reposition");
				// this._strict_bounds = this.map.getBounds();
			}
		}
	},

	disableMapActions : function ()
	{
		this.map.disableDragging();
		this.map.disableMouseZoom();
		
		this._map_actions_enabled = false;
	},

	enableMapActions : function ()
	{
		this.map.enableDragging();
		this.map.enableMouseZoom();
		
		this._map_actions_enabled = true;
	},

	getBounds : function()
	{
		return this.map.getBounds();
	},

	addMarker : function (marker)
	{
		this.map.addOverlay(marker);
		this._markers[this._markers.length] = marker;
	},

	getMarker : function (key, value)
	{
		for (var i = 0; i < this._markers.length; i++)
		{
			var m = this._markers[i];
			if (m[key] && (m[key] == value))
				return m;
		}
		return null;
	},

	clearMarkers : function (except_marker)
	{
		if (this._enlargeOnMarker && (except_marker != this._enlargeOnMarker))
		{
			try { this.map.removeOverlay(this._enlargeOnMarker); } catch (ex) {}
			this._enlargeOnMarker = null;
		}
		for (var i = 0; i < this._markers.length; i++)
		{
			if (except_marker && (this._markers[i] == except_marker))
					continue;

			this.map.removeOverlay(this._markers[i]);
		}
		delete this._markers;
		this._markers = [];

		if (except_marker)
			this._markers[this._markers.length] = except_marker;
	},

	mapSetCenter : function (lat_lng, zoom, _trigger_ev)
	{
		if (!_trigger_ev)
			this._code_event = true;
		this.map.setCenter(lat_lng, zoom);
		this._code_event = false;
	},

	getBoundsAsString : function (bounds)
	{
		var strout = "";
		for (var x in bounds)
		{
			if (typeof(bounds[x]) == "function")
				continue;
			strout += x + "=" + bounds[x] + "\n";
			if (typeof(bounds[x]) == "object")
			{
				for (var y in bounds[x])
				{
					if (typeof(bounds[x][y]) == "function")
						continue;
					strout += "    " + y + "=" + bounds[x][y] + "\n";
				}
			}
		}
		return strout;
	},

	mapSaveBounds : function ()
	{
		this._saved_bounds_user = this.map.getBounds();
		return this._saved_bounds_user;
	},

	mapSetSavedBounds : function (bounds)
	{
		this._saved_bounds_user = bounds;
		return this._saved_bounds_user;
	},

	mapGetSavedBounds : function ()
	{
		return this._saved_bounds_user;
	},

	mapRestoreBounds : function ()
	{
		if (this._saved_bounds_user)
		{
			 this.mapZoomToBounds(this._saved_bounds_user);
			 return this._saved_bounds_user;
		}
	},

	mapUnsetSavedBounds : function ()
	{
		this._saved_bounds_user = null;
	},

	mapZoomToBounds : function (bounds, _trigger_ev)
	{
		if (!_trigger_ev)
			this._code_event = true;
		this.map.zoomToBounds(bounds);
		this._code_event = false;
	},

	getGeoCoder : function ()
	{
		if (!this._map_geocoder)
			this._map_geocoder = new CM.Geocoder(this._api_key);
		return this._map_geocoder;
	},

	checkMoveEndedEvent : function ()
	{
		var ref_this = this;
		if (this.lastMapMove && (((new Date()).getTime() - this.lastMapMove) > 800))
		{
			// this.lastMapMove = (new Date()).getTime();
			if (((new Date()).getTime() - this.lastMapMoveTriggered) > 2000)
			{
				this.lastMapMoveTriggered = (new Date()).getTime();
				this.trigger( "ev_mapAreaChanged", this );
			}
			else
			{
				// check back a bit later
				this._tmp_timeout = setTimeout(function () {ref_this.checkMoveEndedEvent();}, 500);
			}
		}
		else
		{
			// check back a bit later
			this._tmp_timeout = setTimeout(function () {ref_this.checkMoveEndedEvent();}, 500);
		}
	},

	highlightSearchArea : function (search_data, center_on, callback_function, callback_object)
	{
		this.clearSearchOverlay();

		var ref_this = this;

		// alert(search_data);
		this.getGeoCoder().getLocations(search_data,  // "country:Romania",
			function(response)
			{
				var coords = null;
				if (response.features[0].geometry)
					coords = response.features[0].geometry.coordinates[0][0];
				var c_coords = response.features[0].centroid.coordinates;

			    var sw = new CM.LatLng(response.bounds[0][0], response.bounds[0][1]),
			        ne = new CM.LatLng(response.bounds[1][0], response.bounds[1][1]);
			        
			        
			    /*alert(response.bounds[0][0] + "x" + response.bounds[0][1] + "x" + response.bounds[1][0] + 
			    	"x" + response.bounds[1][1]);*/

			    if ((!jQuery.browser.msie) && coords)
			    {
					var cm_coords = [];

					for (var q in coords)
						cm_coords[cm_coords.length] = new CM.LatLng(coords[q][0], coords[q][1]);

					var polygon = new CM.Polygon(cm_coords, "#ff5303", 1, 0.5, "#555555", 0.07);

					ref_this.map.addOverlay(polygon);
					ref_this._search_overlay = polygon;
			    }

			    // alert(center_on);

			    /*alert(response.bounds[0][0] + "x" + response.bounds[0][1] + "x" + response.bounds[1][0] + "x" +
			    	response.bounds[1][1]);*/

				if (center_on)
					ref_this.mapZoomToBounds(new CM.LatLngBounds(
				    	new CM.LatLng(response.bounds[0][0], response.bounds[0][1]),
						new CM.LatLng(response.bounds[1][0], response.bounds[1][1])
				    	));

				if (callback_function && callback_object)
				{
					callback_function.call(callback_object, new CM.LatLngBounds(
				    	new CM.LatLng(response.bounds[0][0], response.bounds[0][1]),
						new CM.LatLng(response.bounds[1][0], response.bounds[1][1])
				    	));
				}
			}, {geometry : false});
	},

	clearSearchOverlay : function()
	{
		if (this._search_overlay)
			this.map.removeOverlay(this._search_overlay);
	},

	mapLoaded : function ()
	{
		//
	},

	addMapAreaListener : function (callback_function, e_data)
	{
		this.bind("ev_mapAreaChanged", e_data, callback_function);
	},

	removeMapAreaListener : function (callback_function)
	{
		this.unbind("ev_mapAreaChanged", callback_function);
	},

	updateBoundsChanged : function()
	{
		try
		{
			this.map.checkResize();
			this.mapSetCenter(this.map.getCenter(), this.map.getZoom());
			this.map.checkResize();
		}
		catch (ex)
		{
			alert(ex);
		}
	},

	onUpdateDisplayImages : function (event)
	{
		this.setImageMode(event.is_checked, false);
	},

	setImageMode : function (show_images, update_checkbox)
	{
		show_images = show_images ? true : false;
		this.imageMode = show_images;
		// this.trigger( "ev_mapAreaChanged", this );
		if (update_checkbox)
			jQuery(this.domElement).find(".QMapCMImageControlTable input").attr('checked', show_images);
	},

	onUpdateDisplayLayer : function (event)
	{
		// alert("onUpdateDisplayLayer");
		// this.map.setTileLayer(new CM.Tiles.OpenStreetMap.Mapnik());
		if (event.is_checked)
			this.map.setTileLayer(new CM.Tiles.OpenStreetMap.Cycle());
		else
		{
			if (this._style_id)
				this.map.setTileLayer(new CM.Tiles.CloudMade.Web({key: this._api_key, styleId: this._style_id}));
			else
				this.map.setTileLayer(new CM.Tiles.CloudMade.Web({key: this._api_key}));
		}
	},

	openOverlayInfoWindow : function(html, css)
	{
 		this.getOverlayInfoWindow().find("." + this.getOverlayInfoWindowId() + "-content").html(html);
		this.getOverlayInfoWindow().css(css).show();
	},

	closeOverlayInfoWindow : function()
	{
		this.getOverlayInfoWindow().hide();
	},

	getOverlayInfoWindow : function()
	{
		return jQuery(this.domElement).find("#" + this.getOverlayInfoWindowId());
	},

	getOverlayInfoWindowId : function()
	{
		return this.fullId + "__info";
	}
});


var QMapCMImageControl = QObject.extend('QMapCMImageControl', {

	id : null,

	initialize: function(map)
	{
		this.id = map._jsCtrl.fullId + "-QMapCMImageControl";

		var container = document.createElement('div');

		jQuery(container).attr("id", this.id);
		jQuery(container).css("padding", "3px").css("position", "relative");

		jQuery(container).html("<div class='QMapCMImageControlBg'><!-- --></div><table class='QMapCMImageControlTable'><tr><td valign='middle'><input type='checkbox' name='" + this.id +
				"-chk' value='true' id='" + this.id + "-chk' checked='false' /></td><td valign='middle'><label for='" +
				this.id + "-chk'>&nbsp;Images</label></td></tr></table>");

		jQuery(container).find(".QMapCMImageControlBg").css("background", "none repeat scroll 0 0 white"
			).css("height", "100%").css("left", "0px").css("opacity", "0.7").css("position", "absolute").css("top", "0px"
			).css("width", "100%");

		jQuery(container).find(".QMapCMImageControlTable").css("position", "relative").css("background", "transparent");

		jQuery(container).bind("dblclick", function (event) {event.stopPropagation();})
		jQuery(container).bind("click", function (event) {event.stopPropagation();})

		jQuery(container).find("input").bind("change", function (event)
			{
				jQuery(map._jsCtrl).trigger({type: "ev_ImageControlChanged", is_checked: (jQuery(container).find("input:checked").length > 0)});
			});

		map.getContainer().appendChild(container);
		return container;
	},

	getDefaultPosition: function()
	{
		return new CM.ControlPosition(CM.TOP_RIGHT, new CM.Size(1, 0));
	}
});

var QMapCMLayerControl = QObject.extend('QMapCMLayerControl', {

	id : null,

	initialize: function(map)
	{
		this.id = map._jsCtrl.fullId + "-QMapCMLayerControl";

		var container = document.createElement('div');

		jQuery(container).attr("id", this.id);
		jQuery(container).css("padding", "3px").css("position", "relative");

		jQuery(container).html("<div class='QMapCMLayerControlBg'><!-- --></div><table class='QMapCMLayerControlTable'><tr><td valign='middle'><input type='checkbox' name='" + this.id +
				"-chk' value='true' id='" + this.id + "-chk' /></td><td valign='middle'><label for='" +
				this.id + "-chk'>&nbsp;Relief</label></td></tr></table>");

		jQuery(container).find(".QMapCMLayerControlBg").css("background", "none repeat scroll 0 0 white"
			).css("height", "100%").css("left", "0px").css("opacity", "0.7").css("position", "absolute").css("top", "0px"
			).css("width", "100%");

		jQuery(container).find(".QMapCMLayerControlTable").css("position", "relative").css("background", "transparent");

		jQuery(container).bind("dblclick", function (event) {event.stopPropagation();})
		jQuery(container).bind("click", function (event) {event.stopPropagation();})

		jQuery(container).find("input").bind("change", function (event)
			{
				jQuery(map._jsCtrl).trigger({type: "ev_LayerControlChanged", is_checked: (jQuery(container).find("input:checked").length > 0)});
			});

		map.getContainer().appendChild(container);
		return container;
	},

	getDefaultPosition: function() {
		return new CM.ControlPosition(CM.TOP_RIGHT, new CM.Size(0, 0));
	}
});

var QMapCMCenterControl = QObject.extend('QMapCMCenterControl', {

	id : null,

	initialize: function(map)
	{
		this.id = map._jsCtrl.fullId + "-QMapCMCenterControl";

		var container = document.createElement('div');

		jQuery(container).attr("id", this.id);
		jQuery(container).css("padding", "3px").css("position", "relative");

		jQuery(container).html("<div class='QMapCMCenterControlBg'><!-- --></div>" +
				"<div class='QMapCMCenterControlInput'><input type='button' name='" + this.id +
				"-chk' value='Center' id='" + this.id + "-chk' /></div>");

		jQuery(container).find(".QMapCMCenterControlBg").css("background", "none repeat scroll 0 0 white"
			).css("height", "100%").css("left", "0px").css("opacity", "0.7").css("position", "absolute").css("top", "0px"
			).css("width", "100%");

		jQuery(container).find(".QMapCMCenterControlInput").css("position", "relative").css("background", "transparent");

		jQuery(container).bind("dblclick", function (event) {event.stopPropagation();})
		jQuery(container).bind("click", function (event) {event.stopPropagation();})

		jQuery(container).find("input").bind("click", function (event)
			{
				jQuery(map._jsCtrl).trigger({type: "ev_CenterControlChanged", is_checked: (jQuery(container).find("input:checked").length > 0)});
			});

		map.getContainer().appendChild(container);
		return container;
	},

	getDefaultPosition: function() {
		return new CM.ControlPosition(CM.BOTTOM_LEFT, new CM.Size(0, 0));
	}
});

var QMapCMEnlargeControl = QObject.extend('QMapCMEnlargeControl', {

	id : null,
	map : null,
	
	setCaption : function (caption)
	{
		// alert(caption);
		jQuery("#" + this.id + " input", this.map._jsCtrl.domElement).val(caption);
	},

	initialize: function(map)
	{
		this.id = map._jsCtrl.fullId + "-QMapCMEnlargeControl";
		this.map = map;

		var container = document.createElement('div');

		jQuery(container).attr("id", this.id);
		jQuery(container).css("padding", "3px").css("position", "relative");

		jQuery(container).html("<div class='QMapCMEnlargeControlBg'><!-- --></div>" +
				"<div class='QMapCMEnlargeControlInput'><input type='button' name='" + this.id +
				"-chk' value='" + map._jsCtrl._texts["expand"] + "' id='" + this.id + "-chk' /></div>");

		jQuery(container).find(".QMapCMEnlargeControlBg").css("background", "none repeat scroll 0 0 white"
			).css("height", "100%").css("left", "0px").css("opacity", "0.7").css("position", "absolute").css("top", "0px"
			).css("width", "100%");

		jQuery(container).find(".QMapCMEnlargeControlInput").css("position", "relative").css("background", "transparent");

		jQuery(container).bind("dblclick", function (event) {event.stopPropagation();})
		jQuery(container).bind("click", function (event) {event.stopPropagation();})

		jQuery(container).find("input").bind("click", function (event)
			{
				jQuery(map._jsCtrl).trigger({type: "ev_EnlargeControlChanged", is_checked: (jQuery(container).find("input:checked").length > 0)});
			});

		map.getContainer().appendChild(container);
		return container;
	},

	getDefaultPosition: function() {
		return new CM.ControlPosition(CM.BOTTOM_RIGHT, new CM.Size(0, 0));
	}
});

// CM.Marker

CM.Marker.prototype.openOverlayInfoWindow = function(html)
{
	// this.getIcon().iconAnchor.x;
	// this.getIcon().iconAnchor.y;

	var pos = jQuery(this._image).position();

	var container_pos = jQuery(this._image).parent().parent().position();
	pos.left += container_pos.left;
	pos.top += container_pos.top;

	var info = this._map._jsCtrl.getOverlayInfoWindow();
	/*if (jQuery.browser.msie)
		info.css("width", "220px");*/
	info.find("." + this._map._jsCtrl.getOverlayInfoWindowId() + "-content").html(html);

	// need to fix this !
	info.show();

	var left_pos = pos.left - info.width() - 8;
	var top_pos = pos.top - 9;
	// info.hide();

	info.css({left : left_pos + "px", top: top_pos + "px"});
	// info.show();
}

CM.Marker.prototype.closeOverlayInfoWindow = function()
{
	// alert("prototype.closeOverlayInfoWindow");
	this._map._jsCtrl.closeOverlayInfoWindow();
}

/*
function printStackTrace()
{
  var callstack = [];
  var isCallstackPopulated = false;
  try {
    i.dont.exist+=0; //doesn't exist- that's the point
  } catch(e) {
    
  	// alert(e.stack);
  	
  	if (e.stack) { //Firefox
      var lines = e.stack.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          callstack.push(lines[i]);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
    else if (window.opera && e.message) { //Opera
      var lines = e.message.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          var entry = lines[i];
          //Append next line also since it has the file info
          if (lines[i+1]) {
            entry += " at " + lines[i+1];
            i++;
          }
          callstack.push(entry);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
  }
  if (!isCallstackPopulated) { //IE and Safari
    var currentFunction = arguments.callee.caller;
    while (currentFunction) {
      var fn = currentFunction.toString();
      var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf('')) || 'anonymous';
      callstack.push(fname);
      currentFunction = currentFunction.caller;
    }
  }
  output_arr(callstack);
}

function output_arr(arr)
{
  //Optput however you want
  alert(arr.join('\n\n'));
}

*/

